




package ch.hearc.java.jmapviewer.example;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;

import javax.swing.JComboBox;
import javax.swing.JPanel;

import org.openstreetmap.gui.jmapviewer.DefaultMapController;
import org.openstreetmap.gui.jmapviewer.JMapViewer;
import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
import org.openstreetmap.gui.jmapviewer.tilesources.BingAerialTileSource;
import org.openstreetmap.gui.jmapviewer.tilesources.OsmTileSource;

/***
 * MapPanel which contains the JMapViewer
 * @author Adrian Moll
 *
 */
public class JPanelMap extends JPanel
	{

	/*------------------------------------------------------------------*\
	|*							Constructeurs							*|
	\*------------------------------------------------------------------*/
	public JPanelMap()
		{
		geometry();
		control();
		apparence();
		}

	/*------------------------------------------------------------------*\
	|*							Methodes Public							*|
	\*------------------------------------------------------------------*/

	public void addMarker(JPanelMapMarker marker)
		{
		mapViewer.addMapMarker(marker);
		mapViewer.add(marker);
		}

	/*------------------------------------------------------------------*\
	|*							Methodes Private						*|
	\*------------------------------------------------------------------*/
	private void geometry()
		{
		this.setLayout(new BorderLayout());
		mapViewer = new JMapViewer();

		comboboxTileSource = new JComboBox<TileSource>();
		TileSource mapTileSource = new OsmTileSource.Mapnik();
		comboboxTileSource.insertItemAt(mapTileSource, 0);
		comboboxTileSource.insertItemAt(new BingAerialTileSource(), 1);
		comboboxTileSource.setSelectedIndex(0);
		JPanel panel = new JPanel();
		comboboxTileSource.setSize(200, 20);
		comboboxTileSource.setLocation(0, 0);
		panel.setPreferredSize(comboboxTileSource.getSize());
		panel.setLayout(null);
		panel.add(comboboxTileSource);

		this.add(mapViewer, BorderLayout.CENTER);
		this.add(panel, BorderLayout.NORTH);

		}

	private void control()
		{
		mapController = new DefaultMapController(mapViewer);
		mapController.setMovementMouseButton(MouseEvent.BUTTON1);

		comboboxTileSource.addActionListener(new ActionListener()
			{

				@Override
				public void actionPerformed(ActionEvent event)
					{
					mapViewer.setTileSource((TileSource)comboboxTileSource.getSelectedItem());
					}
			});
		}

	private void apparence()
		{
		mapViewer.setDisplayPositionByLatLon(46.957761, 8.272705, 8);
		}

	/*------------------------------------------------------------------*\
	|*							Attributs Private						*|
	\*------------------------------------------------------------------*/

	//Tools
	private JMapViewer mapViewer;
	private DefaultMapController mapController;
	private JComboBox<TileSource> comboboxTileSource;
	}
